package cn.wp.ewallet.wallet.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.wp.common.core.id.Id;
import cn.wp.ewallet.wallet.dao.StatInfoDao;
import cn.wp.ewallet.wallet.dao.TransAccountLogDao;
import cn.wp.ewallet.wallet.model.entity.StatInfoDo;
import cn.wp.ewallet.wallet.model.entity.TransAccountLogDo;
import cn.wp.ewallet.wallet.service.FeeStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import static cn.wp.ewallet.wallet.extension.constant.EwalletWalletConstant.*;

@Service
@Slf4j
public class FeeStatisticsServiceImpl implements FeeStatisticsService {

    @Autowired
    private TransAccountLogDao transAccountLogDao;

    @Autowired
    private StatInfoDao statInfoDao;

    @Override
    public void feeStatistices() {
        //查询昨日的统计信息
        List<TransAccountLogDo> transAccountLogDoList = transAccountLogDao.selectYesterdayTransAccountLogList();
        BigDecimal statTypeSHQCTransServiceCharge = BigDecimal.valueOf(0);
        BigDecimal statTypeUSDTServiceCharge = BigDecimal.valueOf(0);
        BigDecimal statTypeSHQCMentionServiceCharge = BigDecimal.valueOf(0);
        for (TransAccountLogDo transAccountLogDo : transAccountLogDoList) {
            if (transAccountLogDo.getOperType().equals(TRANS_LOG_TYPE_SYSTEM)) {
                statTypeSHQCTransServiceCharge = statTypeSHQCTransServiceCharge.add(transAccountLogDo.getServiceCharge());
            }
            if (transAccountLogDo.getOperType().equals(TRANS_LOG_TYPE_MENTION_MONEY) && transAccountLogDo.getMoneyTypeId().equals(MONEY_TYPE_USDT)) {
                statTypeUSDTServiceCharge = statTypeUSDTServiceCharge.add(transAccountLogDo.getServiceCharge());
            }
            if (transAccountLogDo.getOperType().equals(TRANS_LOG_TYPE_MENTION_MONEY) && transAccountLogDo.getMoneyTypeId().equals(MONEY_TYPE_SHQC)) {
                statTypeSHQCMentionServiceCharge = statTypeSHQCMentionServiceCharge.add(transAccountLogDo.getServiceCharge());
            }
        }
        StatInfoDo previousSHQCTransStatInfoDo = statInfoDao.selectstatInfoByStatType(STAT_TYPE_SHQC_TRANS_ACCOUNT_SERVICE_CHARGE, getDateStr(new Date()));
        StatInfoDo statInfoDo = createStatInfo(STAT_TYPE_SHQC_TRANS_ACCOUNT_SERVICE_CHARGE);
        if (previousSHQCTransStatInfoDo == null) {
            statInfoDo.setYesCurrency(statTypeSHQCTransServiceCharge);
            statInfoDo.setTotalCurrency(statTypeSHQCTransServiceCharge);
        } else {
            statInfoDo.setYesCurrency(statTypeSHQCTransServiceCharge);
            statInfoDo.setTotalCurrency(previousSHQCTransStatInfoDo.getTotalCurrency().add(statTypeSHQCTransServiceCharge));
        }
        statInfoDao.insertStatInfo(statInfoDo);
        StatInfoDo previousUSDTMentionStatInfoDo = statInfoDao.selectstatInfoByStatType(STAT_TYPE_USDT_MENTION_SERVICE_CHARGE, getDateStr(new Date()));
        StatInfoDo statInfo = createStatInfo(STAT_TYPE_USDT_MENTION_SERVICE_CHARGE);
        if (previousUSDTMentionStatInfoDo == null) {
            statInfo.setYesCurrency(statTypeUSDTServiceCharge);
            statInfo.setTotalCurrency(statTypeUSDTServiceCharge);
        } else {
            statInfo.setYesCurrency(statTypeUSDTServiceCharge);
            statInfo.setTotalCurrency(previousUSDTMentionStatInfoDo.getTotalCurrency().add(statTypeUSDTServiceCharge));
        }
        statInfoDao.insertStatInfo(statInfo);
        StatInfoDo previousSHQCMentionStatInfoDo = statInfoDao.selectstatInfoByStatType(STAT_TYPE_SHQC_MENTION_SERVICE_CHARGE, getDateStr(new Date()));
        StatInfoDo statInfo1 = createStatInfo(STAT_TYPE_SHQC_MENTION_SERVICE_CHARGE);
        if (previousSHQCMentionStatInfoDo == null) {
            statInfo1.setYesCurrency(statTypeSHQCMentionServiceCharge);
            statInfo1.setTotalCurrency(statTypeSHQCMentionServiceCharge);
        } else {
            statInfo1.setYesCurrency(statTypeSHQCMentionServiceCharge);
            statInfo1.setTotalCurrency(statTypeSHQCMentionServiceCharge.add(previousSHQCMentionStatInfoDo.getTotalCurrency()));
        }
        statInfoDao.insertStatInfo(statInfo1);
    }

    public String getDateStr(Date date) {
        DateTime dateTime = DateUtil.offsetDay(date, -2);
        return DateUtil.format(dateTime, "yyyy-MM-dd");
    }
    public StatInfoDo createStatInfo(String type){
        StatInfoDo statInfoDo =new StatInfoDo();
        statInfoDo.setStatId(Id.gen());
        statInfoDo.setStatDate(DateUtil.offsetDay(new Date(), -1));
        statInfoDo.setStatType(type);
        return statInfoDo;
    }

}
